
<template>
    <span class="txt-show">{{ tip_content }}</span>
</template>

<script setup>

import { Command } from '@tauri-apps/plugin-shell';
import { inject,nextTick,ref } from 'vue';

const g_ws = inject('g_ws4ser')
const who  = inject('who')
const advv = inject('advv')
const serve_addr = inject('serve_addr')
const deng = inject('led_sta')

let turn_mid_timer = null  //有左转或右转指令时，要事后发个回中间的指令
let led_sta = false 
let uart1_ws = new WebSocket(`ws://${serve_addr.value}:10088/m32_cmd`) 

let kZ_tmr = null 
let comp_n = 0
const tip_content = ref('')

const emit = defineEmits(['btnC'])

function send_turn_mid_cmd(){
    const data= [0xaa,0xaa,0x40,8,  0x3,0xe8,/*第1-2字节 值为1000*/0x3,0xe8,
                     0x3,0xe8,/*第5-6字节 值为1000 回正指令*/  0x3,0xe8, 0x48/*校验和为0x548 取低字节*/]
    const cmdData = new Uint8Array(data)//Buffer.from(data)
    if(g_ws.conn && g_ws.conn.readyState==1){
        g_ws.conn.send(cmdData)
    }
    turn_mid_timer = null
}

async function handle_joy_input(ser_data){
    let data=''
    if(ser_data.match('poweroff')){
        await Command.create('shut-down', ['/s','/t','0']).execute()
        //console.log("EXECUTE ret:",result)
        return 
    }
    if(ser_data.match('L<')){
        if(g_ws.conn && g_ws.conn.readyState==1){
            data=[0xaa,0xaa,0x40,8,  0x3,0xe8,/*第1-2字节 值为1000*/ 0x3,0xe8,
                0x1,0xb0,/*第5-6字节 值为432*/  0x3,0xe8, 0x0e/*校验和为0x50e 取低字节*/]
            const cmdData = new Uint8Array(data)//Buffer.from(data)
            g_ws.conn.send(cmdData)
            if(turn_mid_timer){
                clearTimeout(turn_mid_timer)
            }
            turn_mid_timer = setTimeout(send_turn_mid_cmd,600)
        }
        return 
    }

    if(ser_data.match('L>')){
        if(g_ws.conn && g_ws.conn.readyState==1){
            data=[0xaa,0xaa,0x40,8,  0x3,0xe8,/*第1-2字节 值为1000*/0x3,0xe8,
                0x5,0xb0,/*第5-6字节 值为1456*/  0x3,0xe8, 0x12/*校验和为0x512 取低字节*/]
            const cmdData = new Uint8Array(data)//Buffer.from(data)
            g_ws.conn.send(cmdData)    
            if(turn_mid_timer){
                clearTimeout(turn_mid_timer)
            }
            turn_mid_timer = setTimeout(send_turn_mid_cmd,600)
        }
        return 
    }

    if(ser_data.match('LA')){
        if(g_ws.conn && g_ws.conn.readyState==1){
            data=[0xaa,0xaa,0x40,8,  0x3,0xe8,/*第1-2字节 值为1000*/0x5,0xb0,/*第3-4字节 值为1456*/
            0x3,0xe8,   0x3,0xe8, 0x12/*校验和为0x512 取低字节*/]
            const cmdData = new Uint8Array(data)//Buffer.from(data)
            g_ws.conn.send(cmdData) 
        }
        return 
    }

    if(ser_data.match('LV')){
        if(g_ws.conn && g_ws.conn.readyState==1){
            data=[0xaa,0xaa,0x40,8,  0x3,0xe8,/*第1-2字节 值为1000*/0x1,0xb0,/*第3-4字节 值为432*/
            0x3,0xe8,   0x3,0xe8, 0x0e/*校验和为0x50e 取低字节*/]
            const cmdData = new Uint8Array(data)//Buffer.from(data)
            g_ws.conn.send(cmdData) 
        }
        return 
    }

    if(ser_data.match('RA')){
        if(g_ws.conn && g_ws.conn.readyState==1){
            data=[0xaa,0xaa,0x41,8, 0,0,0,1,/*模拟雷神手柄的Y键 but[3]位置*/
            0,0,0,0, 0x9e /*校验和为0x19e 取低字节*/]
            const cmdData = new Uint8Array(data)//Buffer.from(data)
            g_ws.conn.send(cmdData)
        }
        return 
    }

    if(ser_data.match('RV')){
        if(g_ws.conn && g_ws.conn.readyState==1){
            data=[0xaa,0xaa,0x41,8, 1,0,0,0,/*模拟雷神手柄的A键 but[0]位置*/
            0,0,0,0, 0x9e /*校验和为0x19e 取低字节*/]
            const cmdData = new Uint8Array(data)//Buffer.from(data)
            g_ws.conn.send(cmdData)
        }
        return 
    }

    if(ser_data.match('R<')){
        if(g_ws.conn && g_ws.conn.readyState==1){
            data=[0xaa,0xaa,0x40,8,  0x3,0xe8,/*第1-2字节 值为1000*/ 0x3,0xe8,
            0x3,0xe8,    0x1,0xb0,/*第7-8字节 值为432*/  0x0e/*校验和为0x50e 取低字节*/]
            const cmdData = new Uint8Array(data)//Buffer.from(data)
            g_ws.conn.send(cmdData)
        }
        return 
    }

    if(ser_data.match('R>')){
        if(g_ws.conn && g_ws.conn.readyState==1){
            data=[0xaa,0xaa,0x40,8,  0x3,0xe8,/*第1-2字节 值为1000*/ 0x3,0xe8,
            0x3,0xe8,    0x5,0xb0,/*第7-8字节 值为1456*/  0x12/*校验和为0x512 取低字节*/]
            const cmdData = new Uint8Array(data)//Buffer.from(data)
            g_ws.conn.send(cmdData)
        }
        return 
    }

    if(ser_data.match('kC')){  //C按键 切换视频源
        setTimeout(()=>{
            emit('btnC')
            //console.log('按键btnC事件~~~')
        },100)

        if(advv.value){
            advv.value = false 
            who.value = true
            return 
        }
        if(!advv.value && who.value) {
            who.value = false 
            return 
        }
        if(!advv.value && !who.value){ 
            advv.value = true 
        }
        
    }

    if(ser_data.match('kW')){// w按键 切换灯泡
        if(!(uart1_ws && uart1_ws.readyState == 1)) return 
        
        if(led_sta){
            uart1_ws.send('led 0\n')
            deng.value = false
        }
        if(!led_sta){
            uart1_ws.send('led 500\n')
            deng.value = true 
        } 
        led_sta = !led_sta         
        
    }

    if(ser_data.match('kZ')){
        if(!(uart1_ws && uart1_ws.readyState == 1)) return 

        if(kZ_tmr){  //连续两次收到kZ按键
            uart1_ws.send('pid_pause \n')
            clearTimeout(kZ_tmr)
            kZ_tmr = null
            tip_content.value = ''
        }
        else if(null == kZ_tmr){
            kZ_tmr = setTimeout(comp_btn_process,2000)
            tip_content.value = '组合键: wait...'
        }
    }

    if(ser_data.match('kB')){
        if(kZ_tmr){
            const x = tip_content.value.length
            comp_n += 1
            tip_content.value = tip_content.value.substring(0,x-1) + String(comp_n)
        }
        

    }
}

// setInterval(() => {
//     deng.value = !deng.value
// }, 2500);

function comp_btn_process(){
    if(!(uart1_ws && uart1_ws.readyState == 1)){
        kZ_tmr = null 
        comp_n = 0
        tip_content.value = ''
        return 
    } 

    if(comp_n>0 && comp_n <4){
        uart1_ws.send(`pid_go ${comp_n} \n`)
        tip_content.value = '发送成功~!'
    }

    kZ_tmr = null 
    comp_n = 0
    setTimeout(()=>{
            tip_content.value = ''
    },350)
}

defineExpose({
    handle_joy_input
})
</script>


<style scoped>
.txt-show{
    background-color: rgba(56, 56, 77, 0.5);
    color: red;
    line-height: 1.2em;
    vertical-align: middle;
    font-size: 0.9em;
    padding-bottom: 0.3em;
    border-radius: 0.6em;
}
</style>
